import glob
ex = glob.glob('/Users/show/PythonAndQA(code)/python-study2025/6_requests/project/bysms/case/*.yaml')
import os
import re
import yaml
from string import Template


for path,dirname,filename in os.walk('/Users/show/PythonAndQA(code)/python-study2025/6_requests'):
    # print('-----------')
    # print(path)
    # print(dirname)
    # print(filename)
    for f in filename:
        if f.endswith('.yaml'):
            print(os.path.join(path,f))
            r = re.match('(.*)\.yaml',f)
            r2 = re.search('(.*)\.yaml',f)
            print(r[1])
            print(r2.group(1))

# 反射 映射
class Example:
    pass

def func1(self):
    print('执行函数1')
   

setattr(Example,'fuc',func1)

ex = Example()
# ex.fuc()
getattr(ex,'fuc')()


def wrapper(para1):
    print(para1)
    def func1(self):
        print('执行了func1')
    return func1

setattr(Example,'fff',wrapper('a'))
ex2 = Example()
ex2.fff()



# yaml占位符替换，这里还是要用迭代的写法，更准确的看r06
def replace(partten,content,data):
    return re.sub(partten,content,data)

def string_replace(source_data,**kwargs):
        data_str = yaml.safe_dump(source_data)
        print(data_str)
        
        for k,v in kwargs.items():
            partten = "\$\{%s\}"%(k,)
            # print(partten)
            
            # s = re.findall(partten,data_str)
            # print(s)
            new = re.sub(partten,str(v),data_str)
            return new

data = [{'username': 'byhy', 'password': '${password}', 'ret': 0, 'mag': '${mag}'}, {'username': 'byhy', 'password': 8888, 'ret': 1, 'mag': '用户名或者密码错误'}, {'username': 'None', 'password': 88888888, 'ret': 1, 'mag': '用户名或者密码错误'}, {'username': 'byhy', 'password': 'None', 'ret': 1, 'mag': '用户名或者密码错误'}]

# n = string_replace(data,password='caipeiyan')
# print(yaml.safe_load(n))

# 用temlplate

def string_tem_replace(source_data,**kwargs):
    data_str = yaml.safe_dump(source_data)
    print(data_str)

    t = Template(data_str)
    new_data = t.substitute(**kwargs)
    print(new_data)
    return yaml.safe_load(new_data)

s = string_tem_replace(data,password='caipeiyan',mag='成功替换')
